package com.kstudio.videocapture.editvideo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.kstudio.videocapture.R;

public class KVideoEditPlayPauseView extends View {

    public static final int PLAY  = 0x0001;
    public static final int PAUSE = 0x0002;

    private static final int RES_PLAY  = R.drawable.kedit_btn_play;
    private static final int RES_PAUSE = R.drawable.kedit_btn_pause;

    private Context context;
    private int State;

    private Drawable mDrawablePlay;
    private Drawable mDrawablePause;

    private int mDrawableWidth;
    private int mDrawableHeight;

    public KVideoEditPlayPauseView(Context context) {
        this(context, null);
    }

    public KVideoEditPlayPauseView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public KVideoEditPlayPauseView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        this.context = context;
        init(attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = mDrawableWidth;
        width = getMeasureSize(widthMeasureSpec, width);
        int height = mDrawableHeight;
        height = getMeasureSize(heightMeasureSpec, height);

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        if(changed) {
            int width = right - left;
            int height = bottom - top;

            int l;
            int t;
            int r;
            int b;

            if( width > mDrawableWidth ) {
                l = ( width - mDrawableWidth ) / 2;
                r =  l + mDrawableWidth;
            } else {
                l = 0;
                r = width;
            }

            if( height > mDrawableHeight ) {
                t = ( height - mDrawableHeight ) / 2;
                b =  t + mDrawableHeight;
            } else {
                t = 0;
                b = height;
            }

            mDrawablePlay.setBounds(l, t, r, b);
            mDrawablePause.setBounds(l, t, r, b);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        switch (State) {
            case PLAY :
                mDrawablePlay.draw(canvas);
                break;
            case PAUSE :
                mDrawablePause.draw(canvas);
            break;
        }
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();

        int[] state = getDrawableState();

        if (mDrawablePlay != null && mDrawablePlay.isStateful()) {
            mDrawablePlay.setState(state);
        }

        if (mDrawablePause != null && mDrawablePause.isStateful()) {
            mDrawablePause.setState(state);
        }

        invalidate();
    }

    private void init(AttributeSet attrs, int defStyle) {
        State = PLAY;

        if(attrs == null) {
            mDrawablePlay = context.getResources().getDrawable(RES_PLAY);
            mDrawablePlay.setCallback(this);
            mDrawablePlay.setState(getDrawableState());
            mDrawablePlay.setVisible(getVisibility() == VISIBLE, false);

            mDrawablePause = context.getResources().getDrawable(RES_PAUSE);
            mDrawablePause.setCallback(this);
            mDrawablePause.setState(getDrawableState());
            mDrawablePause.setVisible(getVisibility() == VISIBLE, false);

            mDrawableWidth  = mDrawablePlay.getIntrinsicWidth() > mDrawablePause.getIntrinsicWidth() ? mDrawablePlay.getIntrinsicWidth() : mDrawablePause.getIntrinsicWidth();
            mDrawableHeight = mDrawablePlay.getIntrinsicHeight() > mDrawablePause.getIntrinsicHeight() ? mDrawablePlay.getIntrinsicHeight() : mDrawablePause.getIntrinsicHeight();

            setClickable(true);
        } else {
            int[] ids = new int[attrs.getAttributeCount()];
            for(int i = 0; i < attrs.getAttributeCount(); i++) {
                ids[i] = attrs.getAttributeNameResource(i);
            }

            TypedArray a = context.obtainStyledAttributes(attrs, ids, defStyle, 0);

            for(int i = 0; i < attrs.getAttributeCount(); i++) {
                String attrName = attrs.getAttributeName(i);
                if (attrName == null)
                    continue;
            }
            a.recycle();
        }
    }

    private int getMeasureSize(int measureSpec, int cSize) {
        int measureMode = MeasureSpec.getMode(measureSpec);
        int measureSize = MeasureSpec.getSize(measureSpec);
        int size = cSize;

        if(measureMode == MeasureSpec.EXACTLY){
            size = measureSize;
        } else if(measureMode == MeasureSpec.UNSPECIFIED || measureMode == MeasureSpec.AT_MOST){
            // Check against our minimum width
            size = Math.max(size, getSuggestedMinimumWidth());

            if (measureMode == MeasureSpec.AT_MOST) {
                size = Math.min(measureSize, size);
            }
        }

        return size;
    }

    public void setState(int s) {
        if(State != s && (s == PLAY || s == PAUSE)) {
            State = s;
            invalidate();
        }
    }

    public int getState() {
        return State;
    }

    public int getDrawableWidth() {
        return mDrawableWidth;
    }

    public int getDrawableHeight() {
        return mDrawableHeight;
    }
}
